package cn.zebra.dev.mall.module.group.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.mall.module.group.dto.ProductGroupDTO;
import cn.zebra.dev.product.module.group.entity.GroupEntity;
import cn.zebra.dev.product.module.group.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "商品分组")
@RestController
@RequestMapping("/group")
public class ProductGroupController {

    @Autowired
    private GroupService groupService;

    @GetMapping("/list")
    @ApiOperation("商品分组")
    public Result<PageUtils<ProductGroupDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page){
        Map<String, Object> params = new HashMap<>(3);
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<GroupEntity> pageUtils = groupService.findPage(params);
        List<GroupEntity> list = pageUtils.getList();
        PageUtils<ProductGroupDTO> data = new PageUtils<>(pageUtils);
        if (list.size() > 0) {
            List<ProductGroupDTO> dtoList = new ArrayList<>();
            list.forEach(entity -> {
                ProductGroupDTO dto = new ProductGroupDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }




}
